package es.udc.empresauriostic.geofinder.view;

import java.io.IOException;
import java.text.DateFormat;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.maps.GeoPoint;

import es.udc.empresauriostic.geofinder.R;
import es.udc.empresauriostic.geofinder.model.Game;
import es.udc.empresauriostic.geofinder.model.ServerAPI;
import es.udc.empresauriostic.geofinder.view.utils.ViewUtils;
import es.udc.empresauriostic.geofinder.view.utils.ViewUtils.AsyncScoresFetcher;

public class GamesOverviewActivity extends SherlockActivity {
	private List<Game> games = new LinkedList<Game>();
	private int offset = 0;
	private static int PAGE_SIZE = 5;
	private SharedPreferences preferences;
	private Button prevButton;
	private Button nextButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.overview_layout);
		preferences = getSharedPreferences(
				getString(R.string.const_string_preferences),
				Activity.MODE_PRIVATE);
		setRowListener(R.id.row0, 0);
		setRowListener(R.id.row1, 1);
		setRowListener(R.id.row2, 2);
		setRowListener(R.id.row3, 3);
		setRowListener(R.id.row4, 4);
		prevButton = ((Button) findViewById(R.id.prev_button));
		if (offset == 0) {
			prevButton.setVisibility(View.INVISIBLE);
		}
		prevButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				offset -= PAGE_SIZE;
				if (offset == 0) {
					prevButton.setVisibility(View.INVISIBLE);
					nextButton.setVisibility(View.VISIBLE);

				}
				new AsyncGameFetcher().execute(offset, PAGE_SIZE);
			}
		});
		nextButton = (Button) findViewById(R.id.next_button);
		nextButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				offset += PAGE_SIZE;
				if (offset != 0) {
					prevButton.setVisibility(View.VISIBLE);
				}
				new AsyncGameFetcher().execute(offset, PAGE_SIZE);
			}
		});
		new AsyncGameFetcher().execute(offset, PAGE_SIZE);
	}

	private void setRowListener(int rowId, final int rowPos) {
		((TableRow) findViewById(rowId))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						if (games.size() > rowPos) {
							long gameId = games.get(rowPos).getId();
							Editor editor = preferences.edit();
							editor.putLong("current_game_id", gameId);
							editor.commit();
						}

						startActivity(new Intent(getApplicationContext(),
								GameActivity.class));
					}
				});
	}

	private class AsyncGameFetcher extends AsyncTask<Integer, Void, Boolean> {
		@Override
		protected Boolean doInBackground(Integer... params) {
			// TODO get location
			try {
				games = ServerAPI.getPublicGames(getApplicationContext(),
						new GeoPoint(1, 2), params[0], params[1]);
				return true;
			} catch (IOException e) {
				return false;
			}
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			if (result) {
				fillRows();
				if (games.size() < 5) {
					nextButton.setVisibility(View.INVISIBLE);
				}
			} else {
				ViewUtils.showToastConnectionProblem(getApplicationContext());
			}
		}

		private void fillRows() {
			int i = 0;
			for (Game game : games) {
				fillRow(i++, game);
			}
			for (int j = i; j < PAGE_SIZE; j++) {
				fillRow(i++, null);
			}
		}

		// TODO comment
		private void fillRow(int rowPos, Game game) {
			TextView zone = null, finishDate = null, playersNumber = null;
			switch (rowPos) {
			case 0:
				zone = ((TextView) findViewById(R.id.row_0_tv_0));
				finishDate = ((TextView) findViewById(R.id.row_0_tv_1));
				playersNumber = ((TextView) findViewById(R.id.row_0_tv_2));
				break;
			case 1:
				zone = ((TextView) findViewById(R.id.row_1_tv_0));
				finishDate = ((TextView) findViewById(R.id.row_1_tv_1));
				playersNumber = ((TextView) findViewById(R.id.row_1_tv_2));
				break;
			case 2:
				zone = ((TextView) findViewById(R.id.row_2_tv_0));
				finishDate = ((TextView) findViewById(R.id.row_2_tv_1));
				playersNumber = ((TextView) findViewById(R.id.row_2_tv_2));
				break;
			case 3:
				zone = ((TextView) findViewById(R.id.row_3_tv_0));
				finishDate = ((TextView) findViewById(R.id.row_3_tv_1));
				playersNumber = ((TextView) findViewById(R.id.row_3_tv_2));
				break;
			case 4:
				zone = ((TextView) findViewById(R.id.row_4_tv_0));
				finishDate = ((TextView) findViewById(R.id.row_4_tv_1));
				playersNumber = ((TextView) findViewById(R.id.row_4_tv_2));
				break;
			}
			if (game != null) {
				zone.setText(game.getZone());
				DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
				finishDate.setText(df.format(game.getFinishDate().getTime()));
				playersNumber.setText(String.valueOf(game.getNumberOfUsers()));
			} else {
				zone.setText("");
				finishDate.setText("");
				playersNumber.setText("");
			}
		}
	}

}
